<template>
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
          <el-row :gutter="15">
            <el-col :lg="6" :md="12">
              <el-form-item label="单据号:">
                <el-input
                  clearable
                  v-model="where.billno"
                  placeholder="请输入单据号"/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="12">
              <div class="ele-form-actions">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  @click="reload">查询
                </el-button>
                <el-button
                type="primary"
              @click="showImport=true"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              >导入
            </el-button>
                <el-button @click="reset">重置</el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :where="where"
          :datasource="dataList"
          :columns="columns"
          :selection.sync="selection"
          height="calc(100vh - 295px)">
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:level:add1')">添加
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:level:dall1')">删除
            </el-button>

            <el-button
              size="small"
              type="success"
              icon="el-icon-download"
              class="ele-btn-icon"
              @click="exportExcel"
              v-if="permission.includes('sys:level:export1')">导出
            </el-button>
          </template>
          <!-- 操作列 -->
          <template slot="action" slot-scope="{row}">
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="openEdit(row)"
              v-if="permission.includes('sys:level:edit1')">修改
            </el-link>
            <el-popconfirm
              class="ele-action"
              title="确定要删除此职级吗？"
              @confirm="remove(row)">
              <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
                v-if="permission.includes('sys:level:delete')">删除
              </el-link>
            </el-popconfirm>
          </template>
          <!-- 状态列 -->
          <template slot="status" slot-scope="{row}">
            <el-switch
              v-model="row.status"
              @change="editStatus(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>
        </ele-pro-table>
      </el-card>
      <!-- 编辑弹窗
      <level-edit
        :data="current"
        :visible.sync="showEdit"
        @done="reload"/> -->
      <!-- 导入弹窗 -->
      <BillImport
        :visible.sync="showImport"
        @done="reload"/>
    </div>
  </template>

  <script>
  import { mapGetters } from "vuex";
  import BillImport from './billimport';

  export default {
    name: 'SysYJImport',
    components: {BillImport},
    computed: {
      ...mapGetters(["permission"]),
    },
    data() {
      return {
        // 表格数据接口
        url: '/ncpaybill/index',
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: "left"
          },
          {
            prop: 'billclass',
            label: '单据大类',
            width: 80,
            align: 'center',
            showOverflowTooltip: true,
            fixed: "left"
          },
          {
            prop: 'billdate',
            label: '推送日期',
            showOverflowTooltip: true,
            width: 160,
            align: 'center',
          },
          {
            prop: 'billmaker',
            label: '制单人',
            sortable: 'custom',
            align: 'center',
            width: 100,
            resizable: false,
          },
          {
            prop: 'billno',
            label: '单据号',
            align: 'center',
            showOverflowTooltip: true,
            width: 160
          },
          {
            prop: 'money',
            label: '总支付金额',
            align: 'center',
            showOverflowTooltip: true,
            width: 100
          },
          {
            prop: 'pkBalatype',
            label: '结算方式',
            align: 'center',
            showOverflowTooltip: true,
            width: 100
          },
          {
            prop: 'pkTradetype',
            label: '付款类型',
            align: 'center',
            showOverflowTooltip: true,
            width: 130
          },
          {
            prop: 'payaccount',
            label: '付款银行账户',
            align: 'center',
            showOverflowTooltip: true,
            width: 180
          },
          {
            prop: 'fkyt',
            label: '付款用途',
            align: 'left',
            showOverflowTooltip: true,
            width: 320
          },
          {
            prop: 'recaccount',
            label: '收款银行账号',
            align: 'center',
            showOverflowTooltip: true,
            width: 150
          },
        //   {
        //     columnKey: 'action',
        //     label: '操作',
        //     width: 150,
        //     align: 'center',
        //     resizable: false,
        //     slot: 'action',
        //     fixed: "right"
        //   }
        ],
        // 表格搜索条件
        where: {},
        // 表格选中数据
        selection: [],
        dataList:[],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示导入弹窗
        showImport: false
      };
    },
    mounted() {
      this.reload();
    },
    methods: {
      /* 刷新表格 */
      reload() {
        this.$http.get('ncpaybill/index').then(res => {
          this.dataList = res.data.data;
        });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
        this.reload();
      },
      /* 显示编辑 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({lock: true});
        this.$http.delete('/level/delete/' + [row.id]).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的职级吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/level/delete/' + [this.selection.map(d => d.id)]).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
      },
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({lock: true});
        let params = Object.assign({
          "id": row.id,
          "status": row.status
        })
        this.$http.put('/level/status', params).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 导出数据Excel */
      exportExcel() {
        let info = JSON.parse(JSON.stringify(this.where));
        this.$http
          .post("/level/exportExcel", info)
          .then((res) => {
            let data = res.data;
            if (data.code == 0) {
              // 下载文件
              window.location.href = process.env.VUE_APP_API_BASE_URL + "/common/download?fileName=" + encodeURI(data.data) + "&isDelete=" + true;
              this.$message({
                type: "success",
                message: "导出成功",
              });
            } else {
              this.$message.error("导出失败");
            }
          })
      },
    }
  }
  </script>

  <style scoped>
  </style>
